<template>
	<view>
		<u-form :model="form" ref="uForm" id="form-container">
			<u-form-item label="工单单别" prop="feedbackOrderType" :label-width="labelWidth" required v-if="form.isCraft">
				<u-input v-model="form.feedbackOrderType" />
			</u-form-item>
			<u-form-item label="工单单号" prop="feedbackWorkOrderNo" :label-width="labelWidth" required v-if="form.isCraft">
				<u-input v-model="form.feedbackWorkOrderNo"/>
			</u-form-item>
			<u-form-item label-width="80" v-if="form.isCraft">
				<u-button type="info" size="mini" @click="searchFeedbackPlanNo">查询排产序号</u-button>
			</u-form-item>
			<u-form-item label="排产序号" prop="feedbackPlanNo" :label-width="labelWidth" required v-if="form.isCraft">
				<u-input v-model="form.feedbackPlanNo"/>
			</u-form-item>
			<u-form-item label="机柜条码" prop="deviceBarcode" :label-width="labelWidth" required>
				<u-input v-model="form.deviceBarcode" :disabled="form.isCraft?false:true" />
			</u-form-item>
			<u-form-item label="维修人员工号" prop="feedbackJobNumber" :label-width="labelWidth" required>
				<u-input v-model="form.feedbackJobNumber"/>
			</u-form-item>
			<u-form-item label-width="80">
				<u-button type="info" size="mini" @click="changeJobNumber">查询维修人员姓名</u-button>
			</u-form-item>
			<u-form-item label="维修人员姓名" prop="feedbackName" :label-width="labelWidth" required>
				<u-input v-model="form.feedbackName" placeholder="输入工号查询" disabled/>
			</u-form-item>
			<u-form-item label="维修标准" prop="standard" :label-width="labelWidth">
				<u-input v-model="form.standard" :type="type" :auto-height="autoHeight" placeholder="请输入维修标准" />
			</u-form-item>
			<u-form-item label="工作中心" prop="workCenter" :label-width="labelWidth" required>
				<u-input v-model="form.workCenter" disabled/>
			</u-form-item>
			<u-form-item label="工序名称" prop="processName" :label-width="labelWidth" required>
				<u-input v-model="form.processName" :disabled="form.isCraft?false:true"/>
			</u-form-item>
			<u-form-item label="工序编号" prop="feedbackProcessOrder" :label-width="labelWidth" required v-if="form.isCraft">
				<u-input v-model="form.feedbackProcessOrder"/>
			</u-form-item>
			<u-form-item class="u-flex u-row-center">
				<u-button type="success" size="medium" @click="submit">提交</u-button>
			</u-form-item>
		</u-form>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import { saveFeedbackJobNumber,getUserByJobNumber,getPlanList } from '../../../utils/index.js'
	export default {
		data() {
			return {
				labelWidth: 160,		//表单每一项的宽度
				autoHeight: true,		//表单“描述”输入框自动高度
				type: 'textarea',		//将描述的输入框设置为textarea
				rules: {						//表单提交前的验证
					deviceBarcode: [{
							required: true, 
							message: '请输入内容', 
							trigger: 'blur',
					}],
					feedbackJobNumber: [{
							required: true, 
							message: '维修人员工号不能为空！', 
							trigger: 'blur',
					}],
					feedbackName: [{
							required: true, 
							message: '维修人员名字不能为空！', 
							trigger: 'blur',
					}],
					feedbackOrderType: [{
							required: true, 
							message: '工单单别不能为空！', 
							trigger: 'blur',
					}],
					feedbackWorkOrderNo: [{
							required: true, 
							message: '工单单号不能为空！', 
							trigger: 'blur',
					}],
					feedbackPlanNo: [{
							required: true, 
							message: '排产序号不能为空！', 
							trigger: 'blur',
					}],
					feedbackProcessOrder: [{
							required: true, 
							message: '工序编号不能为空！', 
							trigger: 'blur',
					}],
				},
				form: {},
				user: uni.getStorageSync('user')
			}
		},
		methods:{
			// 表单验证
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过')
						this.postForm()
					} else {
						this.$refs.uTips.show({
							title: '请完善表单数据!',
							type: 'warning',
							duration: '2000'
						})
					}
				})
			},
			// 提交整个表单，提交按钮绑定的函数
			async postForm(val) {
				let query = Object.assign({},this.form)
				query.sponsorJobNumber = this.user.jobNumber
				query.sponsorName = this.user.userName
				if(query.isCraft) { delete query.isCraft }
				let { data } = await saveFeedbackJobNumber(query)
				if(data.code == '1') {
					this.$refs.uTips.show({
						title: '提交成功！',
						type: 'success',
						duration: '2000'
					})
					setTimeout(
						uni.redirectTo({
						    url: '../../login/login'
					}), 2000)
				} else {
					this.$refs.uTips.show({
						title: data.msg,
						type: 'error',
						duration: '2000'
					})
				}
			},
			async changeJobNumber(val) {
				let query = {
					jobNumber: this.form.feedbackJobNumber
				}
				let { data } = await getUserByJobNumber(query)
				if(data.code == '1') {
					console.log(data.data.userName)
					this.form.feedbackName = data.data.userName
					this.$refs.uTips.show({
						title: '工号查询员工名字成功！',
						type: 'success',
						duration: '2000'
					})
					
				} else {
					this.$refs.uTips.show({
						title: data.msg,
						type: 'error',
						duration: '2000'
					})
				}
			},
			async searchFeedbackPlanNo(){
				if(!this.form.feedbackOrderType) return this.$refs.uTips.show({
					title: '工单单别不能为空!',
					type: 'error',
					duration: '2000'
				})
				if(!this.form.feedbackWorkOrderNo) return this.$refs.uTips.show({
					title: '工单单号不能为空!',
					type: 'error',
					duration: '2000'
				})
				let query = {
					orderType: this.form.feedbackOrderType,
					workOrderNo: this.form.feedbackWorkOrderNo,
				}
				let {data} = await getPlanList(query)
				console.log('排产编号')
				if(data.code == '1') {
					this.form.feedbackPlanNo = data.data.records[0].planNo
				} else {
					this.$refs.uTips.show({
						title: data.msg,
						type: 'error',
						duration: '2000'
					})
				}
			}
		},
		onLoad(option) {
			console.log(option)
			this.form = Object.assign({},option)
			this.form.workCenter = uni.getStorageSync('user').departmentName
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>
	.main-content {
		width: 100%;
	}
	#form-container ::v-deep .u-form-item {
		margin-top: 15rpx;
		padding: 10rpx 30rpx;
	}
	.content {
		padding: 24rpx;
		text-align: center;
	}
	.buttonGap {
		margin-top: 20rpx;
	}
</style>
